Pytest
In this doc, we'll start with a Python project that has an existing pytest test suite, and we'll add JUnit XML as an additional output format for the test suite. JUnit XML contains data such as failing and passing tests, classnames, test files, filenames, test cases, and test execution timestamps. In each step, we'll show the Git diff for the change that we're making.
- Configure
pytest
to use thexunit2
format. Create apytest.ini
file (or update the existing file if your project already has one) and set thejunit_family
as shown below.+[pytest]
+junit_family=xunit2
If you're using pytest
6.1 or later, xunit2
is already the default. As long as you're not currently overriding the default, you can skip this step for pytest
6.1 or later.
- Update your CI workflow to output a JUnit XML file describing the test results.
Below we append the
--junitxml
option when runningpytest
(integration or unit tests), and we tell it to write the report to a file namedjunit.xml
at the root of the project.jobs:
- name: Test with pytest
run: |
pipenv run pytest
pipenv run pytest --junitxml=junit.xml
This example project uses Github Actions for CI, so we're updating our CI script in the `.github/workflows/` directory. If you're using a different CI service, apply this change wherever your CI script is defined (e.g., `.circleci/config.yml` for CircleCI, etc.).
Commit these changes to your repository using the git command line tool.
git commit -am "Update CI to generate JUnit XML for test results"
The final result of these changes should resemble commit 830a749 in the buildpulse-example-pytest repository.
When you run tests or your test script, you should now see generated junit xml files (with tests failed and passed) in your test reports directory for MochaJS - the first step in detecting flaky tests.